enum Player {
  x = "x",
  o = "o",
}

let cells = document.querySelectorAll(".cell");
let bord = document.querySelector("#bord") as Element;
console.log(bord);

interface IArguments {
  [index: number]: any;
  length: number;
}

let win: number[][] = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6],
];

let step: number = 0;
let currentPlayer: Player;
currentPlayer = Player.x;

cells.forEach((item) => {
  let cell = item as HTMLDivElement;
  cell.addEventListener("click", clickCell, { once: true });
});

function clickCell(e: MouseEvent) {
  let target = e.target as HTMLDivElement;
  target.classList.add(currentPlayer);
  step++;
  let isWin = checkWin(currentPlayer);
  if (isWin) {
  return   alert('赢了');
  }
  if (step === 9) {
    return alert("平局");
  }

  // 提示信息
  bord.classList.remove('x','o');
  currentPlayer = currentPlayer == "x" ? Player.o : Player.x;
  bord.classList.add(currentPlayer);
}

function checkWin(player: Player): boolean {
  let isWin = win.some((item) => {
    // 先拿到 判赢数组中的每一个索引 然后 再去拿到 cells中的每一个元素 然后查看是否包含当前玩家
    let cell0 = cells[item[0]];
    let cell1 = cells[item[1]];
    let cell2 = cells[item[2]];
    if (
      cell0.classList.contains(player) &&
      cell1.classList.contains(player) &&
      cell2.classList.contains(player)
    ) {
      return true;
    }
    return false;
  });

  return isWin;
}
